//
//  SuggestMessageViewController.m
//  JiuKuGospel
//
//  Created by jiukuyinyue on 15-12-21.
//  Copyright (c) 2015年 JiuKuForApp. All rights reserved.
//

#import "SuggestMessageViewController.h"

@interface SuggestMessageViewController ()<UITextViewDelegate, UITextFieldDelegate>
@property (nonatomic, strong)UIScrollView * scrollView;
@property (nonatomic, strong)UITextView * textView;
@property (nonatomic, strong)UILabel * placeholderLabel;
@property (nonatomic, strong)UITextField * textField;
@property (nonatomic, strong)UIImageView * selectImageView;
@property (nonatomic, strong)NSString * typeStr;
@end

@implementation SuggestMessageViewController

- (BOOL)canBecomeFirstResponder{
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:NO animated:YES];

    self.title = @"意见反馈";
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 60, 60);
    [rightBtn setTitle:@"提交历史" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightBtn.titleLabel.font = FONT_14;
    [rightBtn addTarget:self action:@selector(rightBtnActionHistory) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    [self setupUI];
    UIButton * butSubmit = [[UIButton alloc]init];
    if (iPhoneX) {
        butSubmit.frame = CGRectMake(15, ScreenHeight-50-HOME_INDICATOR_HEIGHT, ScreenWidth-30, 50);
    }else{
        butSubmit.frame = CGRectMake(15, ScreenHeight-60-HOME_INDICATOR_HEIGHT, ScreenWidth-30, 50);
    }
    butSubmit.backgroundColor = redUIColor;
    [butSubmit setTitle:@"提交" forState:UIControlStateNormal];
    [butSubmit.layer setMasksToBounds:YES];
    [butSubmit.layer setCornerRadius:4];
    [butSubmit addTarget:self action:@selector(rightBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:butSubmit];

}
#pragma mark- 提交的历史记录
-(void)rightBtnActionHistory
{
    if([[_UserM usercode] isEqualToString:@""]){
        pushAccLoginVC;
    }else{
        LSZHistoricalRecordOpinionsVC * vc = [[LSZHistoricalRecordOpinionsVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}
- (void)leftBtnAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)rightBtnAction
{
    if (_textView.text.length == 0) {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:nil message:@"请填写反馈内容" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }else{
        NSString * type  = [[NSString alloc]init];
        if ([self.typeStr isEqualToString:@"产品建议"]) {
            type = @"1";
        }else if ([self.typeStr isEqualToString:@"程序错误"]){
            type = @"2";
        }else{
            type = @"3";
        }
        NSDictionary * infoDic = [[NSBundle mainBundle] infoDictionary];
        CFShow((__bridge CFTypeRef)(infoDic));
        NSString * appVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
        NSString * phoneStr = [[NSString alloc]initWithFormat:@"%@",_textField.text];
        NSString * strC = [[NSString alloc]init];
        strC = [NSString stringWithFormat:@"V%@ %@",appVersion,_textView.text];
        __weak typeof(self) weakSelf = self;
        [LSZRequestData userFeedback:type gnote:strC phone:phoneStr :^(BOOL isOK, NSString * str, NSDictionary * dic) {
            if (isOK ==YES) {
                [weakSelf setStringWarning:@"意见提交成功!"];
            }else{
                [weakSelf setStringWarning:str];
            }
        }];
    }
}
- (void)setupUI
{
    self.selectImageView = [[UIImageView alloc] init];
    if (ScreenWidth == 320) {
        self.selectImageView.frame = CGRectMake(0, 0, 20, 20);
    }else if(ScreenWidth == 375){
        self.selectImageView.frame = CGRectMake(0, 0, 22, 22);
    }else {
        self.selectImageView.frame = CGRectMake(0, 0, 24, 24);
    }
    _selectImageView.image = [UIImage imageNamed:@"我的_设置_关于_意见反馈_yes"];
    _selectImageView.userInteractionEnabled = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;

    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    self.scrollView = [[UIScrollView alloc]init];
    if (iPhoneX) {
        self.scrollView.frame = CGRectMake(0, NAVIGATION_BAR_HEIGHT, self.view.width, self.view.height-50-HOME_INDICATOR_HEIGHT);
    }else{
        self.scrollView.frame = CGRectMake(0, NAVIGATION_BAR_HEIGHT, self.view.width, self.view.height-60-HOME_INDICATOR_HEIGHT);
    }
    [_scrollView addGestureRecognizer:tap];
    [self.view addSubview:_scrollView];

    UILabel * typesLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, _scrollView.width-20, 25)];
    typesLabel.text = @"反馈类型";
    typesLabel.textColor = titleColorLSZ;
    [_scrollView addSubview:typesLabel];
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(typesLabel.frame)+10 , typesLabel.width, 0.3)];
    line.backgroundColor =  [UIColor colorWithRed:190/255.0 green:190/255.0 blue:190/255.0 alpha:1];
    [_scrollView addSubview:line];
    //反馈类型   
    UITapGestureRecognizer * selectTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectType:)];
    UIImageView * chanpinView = [[UIImageView alloc]init];
    if (ScreenWidth == 320) {
        chanpinView.frame = CGRectMake(10, line.bottom+10, 20, 20);
    }else if(ScreenWidth == 375){
        chanpinView.frame = CGRectMake(10, line.bottom+10, 22, 22);
    }else {
        chanpinView.frame = CGRectMake(10, line.bottom+10, 24, 24);
    }
    chanpinView.userInteractionEnabled = YES;
    chanpinView.tag = 1501;
    chanpinView.image = [UIImage imageNamed:@"我的_设置_关于_意见反馈_no"];
    [chanpinView addGestureRecognizer:selectTap];
    [_scrollView addSubview:chanpinView];
    UILabel * chanpinLabel = [[UILabel alloc] initWithFrame:CGRectMake(chanpinView.right+5, chanpinView.top, self.view.width/3-chanpinView.right-5, chanpinView.height)];
    chanpinLabel.text = @"产品建议";
    chanpinLabel.textColor = titleColorLSZ;
    [_scrollView addSubview:chanpinLabel];

    UITapGestureRecognizer * selectTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectType:)];
    UIImageView * chengxuView = [[UIImageView alloc]init];
    if (ScreenWidth == 320) {
        chengxuView.frame = CGRectMake(chanpinLabel.right+10,  chanpinLabel.top, 20, 20);
    }else if(ScreenWidth == 375){
        chengxuView.frame = CGRectMake(chanpinLabel.right+10,  chanpinLabel.top, 22, 22);
    }else {
        chengxuView.frame = CGRectMake(chanpinLabel.right+10,  chanpinLabel.top, 24, 24);
    }
    chengxuView.userInteractionEnabled = YES;
    chengxuView.tag = 1502;
    chengxuView.image = [UIImage imageNamed:@"我的_设置_关于_意见反馈_no"];
    [chengxuView addGestureRecognizer:selectTap1];
    [_scrollView addSubview:chengxuView];
    UILabel * chengxuLabel = [[UILabel alloc] initWithFrame:CGRectMake(chengxuView.right+5, chengxuView.top, self.view.width/3*2-chengxuView.right-5, chengxuView.height)];
    chengxuLabel.text = @"程序错误";
    chengxuLabel.textColor = titleColorLSZ;
    [_scrollView addSubview:chengxuLabel];

    UITapGestureRecognizer * selectTap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectType:)];
    UIImageView * contentView = [[UIImageView alloc] init];
    if (ScreenWidth == 320) {
        contentView.frame = CGRectMake(chengxuLabel.right+10, chengxuLabel.top, 20, 20);
    }else if(ScreenWidth == 375){
        contentView.frame = CGRectMake(chengxuLabel.right+10, chengxuLabel.top, 22, 22);
    }else {
        contentView.frame = CGRectMake(chengxuLabel.right+10, chengxuLabel.top, 24, 24);
    }
    contentView.userInteractionEnabled = YES;
    contentView.tag = 1503;
    contentView.image = [UIImage imageNamed:@"我的_设置_关于_意见反馈_no"];
    [contentView addGestureRecognizer:selectTap2];
    [_scrollView addSubview:contentView];
    UILabel * contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(contentView.right+5, contentView.top, self.view.width-contentView.right-5, contentView.height)];
    contentLabel.text = @"缺少内容";
    contentLabel.textColor = titleColorLSZ;
    [_scrollView addSubview:contentLabel];

    [chanpinView addSubview:_selectImageView];
    self.typeStr = @"产品建议";
    //意见
    self.textView = [[UITextView alloc] init];
    _textView.delegate = self;
    _textView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_textView];
    self.placeholderLabel = [[UILabel alloc] init];
    _placeholderLabel.text = @"您的反馈或建议会使我们的软件更加完美";
    _placeholderLabel.textColor = [UIColor lightGrayColor];
    [_textView addSubview:_placeholderLabel];

    self.textField = [[UITextField alloc] init];
    _textField.delegate = self;
    _textField.placeholder = @"请填写您的联系方式(QQ或手机号)";
    _textField.backgroundColor = [UIColor whiteColor];
    _textField.keyboardType = UIKeyboardTypeNumberPad;
    [_scrollView addSubview:_textField];
    if (ScreenWidth == 320) {
        typesLabel.font = FONT_(13.5);
        chanpinLabel.font = FONT_(13.5);
        chengxuLabel.font = FONT_(13.5);
        contentLabel.font = FONT_(13.5);
        _textView.font = FONT_(13.5);
        _placeholderLabel.font = FONT_(13.5);
        _textField.font = FONT_(13.5);
        self.textView.frame = CGRectMake(10, chanpinView.bottom+10, self.view.width-2*10, 90);
        _placeholderLabel.frame = CGRectMake(5, 5, _textView.width-10, 16);
        self.textField.frame = CGRectMake(_textView.left, _textView.bottom+15, _textView.width, 45);
    }else if(ScreenWidth == 375){
        typesLabel.font = FONT_(14);
        chanpinLabel.font = FONT_(14);
        chengxuLabel.font = FONT_(14);
        contentLabel.font = FONT_(14);
        _textView.font = FONT_(14);
        _placeholderLabel.font = FONT_(14);
        _textField.font = FONT_(14);
        self.textView.frame = CGRectMake(10, chanpinView.bottom+10, self.view.width-2*10, 110);
        _placeholderLabel.frame = CGRectMake(5, 5, _textView.width-10, 18);
        self.textField.frame = CGRectMake(_textView.left, _textView.bottom+15, _textView.width, 50);
    }else {
        typesLabel.font = FONT_(14.5);
        chanpinLabel.font = FONT_(14.5);
        chengxuLabel.font = FONT_(14.5);
        contentLabel.font = FONT_(14.5);
        _textView.font = FONT_(14.5);
        _placeholderLabel.font = FONT_(14.5);
        _textField.font = FONT_(14.5);
        self.textView.frame = CGRectMake(10, chanpinView.bottom+10, self.view.width-2*10, 140);
        _placeholderLabel.frame = CGRectMake(5, 5, _textView.width-10, 20);
        self.textField.frame = CGRectMake(_textView.left, _textView.bottom+15, _textView.width, 58);
    }
}
- (void)tap
{
    _scrollView.contentSize = CGSizeMake(self.view.width, _scrollView.height);
    [self.view endEditing:YES];
}
- (void)selectType:(UITapGestureRecognizer *)tap
{
    UIImageView * tempView = (UIImageView *)tap.view;
    [tempView addSubview:_selectImageView];
    if (tempView.tag == 1501) {
        self.typeStr = @"产品建议";
    }
    if (tempView.tag == 1502) {
        self.typeStr = @"程序错误";
    }
    if (tempView.tag == 1503) {
        self.typeStr = @"缺少内容";
    }
}
-(void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length == 0) {
        _placeholderLabel.hidden = NO;
    }else{
        _placeholderLabel.hidden = YES;
    }
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    if (self.view.height == 480) {
        _scrollView.contentSize = CGSizeMake(self.view.width, self.view.height+70);
    }
    return YES;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (self.view.height == 480) {
        _scrollView.contentSize = CGSizeMake(self.view.width, self.view.height+70);
    }
    return YES;
}
-(void)hiddenMBAction
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    self.textView.text = @"";
    self.textField.text = @"";
    self.placeholderLabel.hidden = NO;
}
@end
